/*
* MIT License
*
* Copyright (c) 2017 wen.gu <454727014@qq.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "tiny_plugin.h"
#include "vos_mem.h"
#include "general_error.h"
#include "test_component1.h"

#define LOG_TAG "TestPlugin1"
#include "vos_log.h"
/***************************************************************************
*
* macro define
*
***************************************************************************/

/***************************************************************************
*
* data structure define
*
***************************************************************************/
typedef struct test_plugin_s
{
    tiny_plugin_t tp;
    /**add private data member for current plugin */
}test_plugin_t;



typedef struct my_component_s
{
    test_component1_t tc;
    /**add private data member for current component */
    GS32 age;
}my_component1_t;

/***************************************************************************
*
* inner API define
*
***************************************************************************/


static void tcRelease(struct tiny_component_s* comp)
{
    if (comp)
    {
        memFree(comp);
    }
}

static void tcSetAge(struct test_component1_s* comp, GS32 age)
{
    if (comp)
    {
        my_component1_t* mc = (my_component1_t*)comp;

        mc->age = age;
    }
}

static void tcSayHello(struct test_component1_s* comp)
{
    if (comp)
    {
        my_component1_t* mc = (my_component1_t*)comp;

        LOGD("i'm a component and my age is: %d\n", mc->age);
    }
}


static tiny_component_t* tcCreate(test_comp_create_param_t* tccp)
{
    my_component1_t* mc = (my_component1_t*)memAlloc(sizeof(my_component1_t));

    if (mc)
    {
        test_component1_t* tc  = (test_component1_t*)mc;
        tiny_component_t* comp = (tiny_component_t*)mc;

        comp->name    = "testComp";
        comp->version = "00.01.00";
        comp->release = tcRelease;

        tc->setAge   = tcSetAge;
        tc->sayHello = tcSayHello;

        mc->age = tccp->age;
    }

    return (tiny_component_t*)mc;
}




///////////////////////////////////////////////////////////////////////////////


static GS32 tpCreateComponent(tiny_plugin_t* tp, tiny_component_t** comp,
                              const tiny_comp_create_param_t* param)
{
    GS32 ret = G_ErrorBadParameter;
    if (tp)
    {
        test_plugin_t* plugin = (test_plugin_t*)(tp);

        if (comp && param)
        {
            if (param->type == TEST_COMP_TYPE)
            {
                *comp = tcCreate((test_comp_create_param_t*)param);

                if (*comp)
                {
                    ret = G_OK;
                }
                else
                {
                    ret = G_ErrorInsufficientResources;
                }
            }
            else
            {
                ret = G_ErrorNotFound;
            }
        }
    }

    return ret;
}


static void tpShutdown(tiny_plugin_t* tp)
{
    if (tp)
    {
        memFree(tp);
    }
}





/***************************************************************************
*
* API define
*
***************************************************************************/


















TINY_PLUGIN_ENTRY()
{
    test_plugin_t* plugin = (test_plugin_t*)memAlloc(sizeof(test_plugin_t));

    if (plugin)
    {
        tiny_plugin_t* tp = (tiny_plugin_t*)(plugin);
        tp->name = "test plugin1";
        tp->version = "00.01.00";
        tp->author = "test@tiny.com";
        tp->createComponent = tpCreateComponent;
        tp->shutdown = tpShutdown;
    }

    return (tiny_plugin_t*)plugin;
}